<?php

namespace App\Http\Controllers;

use EasyWeChat\Factory;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class WxShareController extends Controller
{
    /**
     * Notes: 微信公众号配置开发域名token验证地址（已验证的忽略该方法）.
     */
    public function wxTokenCheck()
    {
        $config = [
            'app_id' => config('wechat.wx_nanwang.app_id'),
            'secret' => config('wechat.wx_nanwang.app_secret'),
            'token'  => 'wechat_token',
            // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
            'response_type' => 'array',
            'log'           => [
                'level' => 'debug',
                'file'  => storage_path('logs') . date('Y-m-d_') . 'wechat.log',
            ],
        ];
        $app      = Factory::officialAccount($config);
        $response = $app->server->serve();
        $response->send(); // Laravel 里请使用：return $response;

        return $response;
    }

    /**
     * Notes: 初始化加载分享页面.
     */
    public function jsConfig(Request $request)
    {
        $config = [
            'app_id' => config('wechat.wx_nanwang.app_id'),
            'secret' => config('wechat.wx_nanwang.app_secret'),
            'token'  => 'wechat_token',
            // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
            'response_type' => 'array',
            'log'           => [
                'level' => 'debug',
                'file'  => storage_path() . '/logs/' . date('Y-m-d_') . 'wechat.log',
            ],
        ];

        $app  = Factory::officialAccount($config);
        $menu = [
            'updateAppMessageShareData',
            'updateTimelineShareData',
            'onMenuShareTimeline',
            'onMenuShareAppMessage',
        ];

        Log::info('test_test', $request->all());

        $url = $request->get('url', '');
        // 以下参数的具体释义参考文档 @link:https://easywechat.com/4.x/
        $data = $app->jssdk->buildConfig($menu, true, false, false, [], $url);

        return response()->json(['data' => $data], 200, [], JSON_UNESCAPED_UNICODE)
            ->header('Access-Control-Allow-Origin', '*')
            ->header('Access-Control-Allow-Methods', '*')
            ->header('Access-Control-Allow-Headers', '*');
    }

    /**
     * Notes: 初始化加载分享页面.
     */
    public function jsConfigSelf()
    {
        $config = [
            'app_id' => config('wechat.wx_self.app_id'),
            'secret' => config('wechat.wx_self.app_secret'),
            'token'  => 'wechat_token',
            // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
            'response_type' => 'array',
            'log'           => [
                'level' => 'debug',
                'file'  => storage_path('logs') . date('Y-m-d_') . 'wechat.log',
            ],
        ];

        $app  = Factory::officialAccount($config);
        $menu = [
            'updateAppMessageShareData',
            'updateTimelineShareData',
            'onMenuShareTimeline',
            'onMenuShareAppMessage',
        ];

        // 以下参数的具体释义参考文档 @link:https://easywechat.com/4.x/
        $data = $app->jssdk->buildConfig($menu, true, false, false);

//        return response()->json(['data' => $data], 200, [], JSON_UNESCAPED_UNICODE);
        // 创建响应对象
        $response = response()->json($data);

        // 设置 CORS 头部
        $response->header('Access-Control-Allow-Origin', '*')
            ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
            ->header('Access-Control-Allow-Headers', 'Content-Type, Authorization');

        return $response;
    }

    /**
     * Notes: 获取分享必要参数.
     */
    private function getJsapi()
    {
    }
}
